package com.micro.modual.extension;

import com.micro.common.MicroConstant;
import com.micro.common.biz.GeneratorCodeUtil;
import com.micro.common.util.SnowFlakeUtil;
import com.micro.modual.entity.SystemElement;
import com.micro.modual.entity.SystemResourceAuth;
import com.micro.modual.model.bo.systemresourceauth.RoleElementBindBO;
import com.micro.modual.model.bo.systemresourceauth.RoleMenuBindBO;
import com.micro.modual.model.bo.systemresourceauth.SystemResourceAuthBO;
import com.micro.modual.model.dto.systemresourceauth.RoleResourceElementDTO;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h1>角色菜单关联表 延展类</h1>
 *
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:23:26
 */
public class SystemResourceAuthExtension {

    /**
     * <h2>角色资源(按钮)相关:角色资源绑定查询</h2>
     * @param elementAuthList
     * @param systemElementList
     * @return
     */
    public static List<RoleResourceElementDTO> converRoleResourceElementListDTO(List<String> elementAuthList, List<SystemElement> systemElementList) {
        List<RoleResourceElementDTO> roleResourceElementDTOList = new ArrayList<>();
        //3.对比判断当角色所分配的功能是否存在被选中的,并返回对应的元素对象列表
        for(SystemElement systemElement : systemElementList){
            RoleResourceElementDTO roleResourceElementDTO = new RoleResourceElementDTO();
            roleResourceElementDTO.setElementCode(systemElement.getElementCode());
            roleResourceElementDTO.setElementName(systemElement.getElementName());
            roleResourceElementDTO.setElementType(systemElement.getElementType());
            roleResourceElementDTO.setElementMethod(systemElement.getElementMethod());
            roleResourceElementDTO.setAuthCode(systemElement.getAuthCode());
            roleResourceElementDTO.setMenuCode(systemElement.getMenuCode());
            if(elementAuthList.contains(systemElement.getElementCode())){
                roleResourceElementDTO.setBindStatus(1);
            }else{
                roleResourceElementDTO.setBindStatus(2);
            }
            roleResourceElementDTOList.add(roleResourceElementDTO);
        }
        return roleResourceElementDTOList;
    }

    /**
     * <h2>角色资源(菜单)相关:角色资源绑定查询</h2>
     * @param roleMenuBindBO
     * @return
     */
    public static List<SystemResourceAuth> converSystemResourceAuthMenu(RoleMenuBindBO roleMenuBindBO) {
        List<SystemResourceAuth> systemResourceAuthList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(roleMenuBindBO.getBindMenuList())){
            for(RoleMenuBindBO.BindMenu bindMenu:roleMenuBindBO.getBindMenuList()){
                SystemResourceAuth systemResourceAuth = new SystemResourceAuth();
                systemResourceAuth.setId(SnowFlakeUtil.getId());
                systemResourceAuth.setRoleCode(roleMenuBindBO.getRoleCode());
                systemResourceAuth.setResourceCode(bindMenu.getMenuCode());
                systemResourceAuth.setAuthCode(bindMenu.getAuthCode());
                systemResourceAuth.setCreateTime(new Date());
                systemResourceAuth.setAuthType("menu");
                systemResourceAuthList.add(systemResourceAuth);
            }
        }
        return systemResourceAuthList;
    }

    /**
     * <h2>角色资源(按钮)相关:角色资源绑定</h2>
     * @param roleElementBO
     * @return
     */
    public static SystemResourceAuth converSystemResourceAuthElement(RoleElementBindBO roleElementBO) {
        SystemResourceAuth systemResourceAuth = new SystemResourceAuth();
        systemResourceAuth.setId(SnowFlakeUtil.getId());
        systemResourceAuth.setAuthCode(roleElementBO.getAuthCode());
        systemResourceAuth.setAuthType(roleElementBO.getElementType());
        systemResourceAuth.setCreateTime(new Date());
        systemResourceAuth.setResourceCode(roleElementBO.getElementCode());
        systemResourceAuth.setRoleCode(roleElementBO.getRoleCode());
        systemResourceAuth.setParentCode(roleElementBO.getMenuCode());
        return systemResourceAuth;
    }

}
